package org.dizena.modules.master.web;

import org.dizena.common.pojo.master.LoginVO;
import org.dizena.common.pojo.master.MenuVO;
import org.dizena.common.pojo.master.PcUserVO;
import org.dizena.common.realm.JwtUtil;
import org.dizena.modules.master.bean.User;
import org.dizena.modules.master.service.MenuService;
import org.dizena.modules.master.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户-接口
 */
@RestController
@RequestMapping("/api/v1/auth")
public class ApiAuthController {
    @Resource
    private UserService service;
    @Resource
    private MenuService menuService;

    /**
     * 当前登录用户明细
     */
    @PostMapping("info")
    public PcUserVO info() {
        User user = service.info();
        PcUserVO vo = new PcUserVO();
        BeanUtils.copyProperties(user, vo);
        return vo;
    }

    /**
     * 当前登录用户
     */
    @PostMapping("who")
    public LoginVO who() {
        User user = service.info();
        LoginVO vo = new LoginVO();
        BeanUtils.copyProperties(user, vo);
        vo.setToken(JwtUtil.sign(user.getAccount(), user.getPasswd()));
        return vo;
    }

    /**
     * 登录用户的菜单
     */
    @GetMapping("menu")
    public List<MenuVO> menu() {
        return menuService.getUserMenu();
    }

    /**
     * 退出登录
     */
    @GetMapping("logout")
    public String logout() {
        return service.logout();
    }


}
